#ifndef SIGNALFILTERIC_H
#define SIGNALFILTERIC_H

#include "constant.h"
#include "IndexCalculator.h"

template <typename FeatureType = double>
struct SignalInfo
{
    FeatureType round_feature; // 轮次特征，模板类型
    ResultType signal_value;

    SignalInfo(FeatureType round_feature, ResultType signal_value)
        : round_feature(round_feature), signal_value(signal_value) {}
};

template <typename FeatureType = double>
class SignalFilterIC : public IndexCalculator<SignalInfo<FeatureType>, ResultType>
{
private:
    bool triggered;

public:
    explicit SignalFilterIC() : IndexCalculator<SignalInfo<FeatureType>, ResultType>(1), triggered(false) {}

    void calculate(const SignalInfo<FeatureType> &new_data) override
    {
        if (new_data.round_feature == this->data[0].round_feature)
        {
            if (triggered)
            {
                this->result = 0;
            }
            else
            {
                if (new_data.signal_value != 0)
                {
                    triggered = true;
                }
                this->result = new_data.signal_value;
            }
        }
        else
        {
            triggered = new_data.signal_value != 0;
            this->result = new_data.signal_value;
        }
    }

    void reset() override
    {
        IndexCalculator<SignalInfo<FeatureType>, ResultType>::reset();
        triggered = false;
    }
};

#endif // SIGNALFILTERIC_H
